package com.jb.workflow.app.bean;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import net.oschina.app.AppException;
import net.oschina.app.bean.Entity;
import net.oschina.app.common.StringUtils;

public class TodoTask extends Entity {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1246182117824018802L;
	
	public final static int CATALOG_CLAIM = 1;//待签收
	public final static int CATALOG_TODO = 2;//待办理
	public final static int CATALOG_ALL = 3;//全部任务
	
	//for order workflow
	private String customer;
	private String orderId;
	private String comment;
	private boolean needDriver = true;

	private String title;
	//任务类型
	private int catalog;
	private String author;
	private int authorId;
	private String pubDate;
	private int isNoticeMe;
	private String step;
	private List<FormData> formDataList = new ArrayList<FormData>(); 
	
	public void setId(int id) {
		this.id = id;
	}
	public String getStep() {
		return step;
	}
	public void setStep(String step) {
		this.step = step;
	}
	public int getCatalog() {
		return catalog;
	}
	public void setCatalog(int catalog) {
		this.catalog = catalog;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public int getAuthorId() {
		return authorId;
	}
	public void setAuthorId(int authorId) {
		this.authorId = authorId;
	}
	public int getIsNoticeMe() {
		return isNoticeMe;
	}
	public void setIsNoticeMe(int isNoticeMe) {
		this.isNoticeMe = isNoticeMe;
	}
	public String getPubDate() {
		return this.pubDate;
	}
	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}
	public String getOrderId() {
		return orderId;
	}
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	public String getCustomer() {
		return customer;
	}
	public void setCustomer(String customer) {
		this.customer = customer;
	}
	
	public boolean getNeedDriver() {
		return needDriver;
	}
	public void setNeedDriver(boolean needDriver) {
		this.needDriver = needDriver;
	}
	
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	
	//订单工作流专用，设置订单号，客户，备注
	public void setOrderData(InputStream inputStream) throws IOException, AppException {
		
		String result = StringUtils.toConvertString(inputStream);
		
		try {
			JSONObject object = new JSONObject(result);
			if (object != null) {
				JSONArray arr = (JSONArray) object.get("formProperties");
				for (int i = 0; i < arr.length(); i++) {
					JSONObject obj = (JSONObject) arr.get(i);
					if (obj != null) {
						String id = obj.getString("id");
						if ("orderId".equals(id)) {
							orderId = obj.getString("value");
						} else if ("customer".equals(id)) {
							customer = obj.getString("value");
						} else if ("comment".equals(id)) {
							comment = obj.getString("value");
						} else if ("needDriver".equals(id)) {
							needDriver = !("false".equals(obj.getString("value")));
						}
					}
				}
			}

		} catch (JSONException e) {
			throw AppException.json(e);
		} finally {
			inputStream.close();
		}
	}
	
	
	//通用的获取Form Data方法
	public void setFormData(InputStream inputStream) throws IOException, AppException {
		
		String result = StringUtils.toConvertString(inputStream);
		
		try {
			JSONObject object = new JSONObject(result);
			if (object != null) {
				JSONArray arr = (JSONArray) object.get("formProperties");
				for (int i = 0; i < arr.length(); i++) {
					JSONObject obj = (JSONObject) arr.get(i);
					if (obj != null) {
						formDataList.add(FormData.parse(obj));
					}
				}
			}
			
			for (FormData formData: formDataList) {
				if (formData.getId().equals("orderId")) {
					orderId = formData.getValue();
				} else if (formData.getId().equals("customer")) {
					orderId = formData.getValue();
				}
			}
		} catch (JSONException e) {
			throw AppException.json(e);
		} finally {
			inputStream.close();
		}
	}
	
	public static TodoTask parse(InputStream inputStream) throws IOException, AppException {
		TodoTask task = null;
        //获得XmlPullParser解析器
        //XmlPullParser xmlParser = Xml.newPullParser();
        try {        	

        } catch (Exception e) {
			throw AppException.xml(e);
        } finally {
        	inputStream.close();	
        }      
        return task;       
	}

}
